package com.seanLab.tool.es;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.seanLab.domain.Image;
import com.seanLab.dto.RecommendImageDto;
import com.seanLab.dto.RecommendTagInfoDto;
import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;

/**
 * Created by mac on 17/9/28.
 */
public class UtilJson {


    public static RecommendImageDto json2RecommendImageDto(JSONObject o){
        ObjectMapper mapper = new ObjectMapper();
        ArrayList list=new ArrayList<RecommendTagInfoDto>();
        JSONArray array=o.getJSONArray("Tags");
        for(int i=0;i<array.length();i++){
            list.add(json2RecommendTagInfo(array.getJSONObject(i)));//TODO
        }
        RecommendImageDto imageDto=new RecommendImageDto();
        imageDto.setImgID(o.getString("img_id"));
        imageDto.setImgURL(o.getString("imgLocalURL"));
        imageDto.setImgOriginalURL(o.getString("imgOriginalURL"));
        imageDto.setImgSource(o.getString("img_source"));
        imageDto.setImgTagList(list);
        return imageDto;
    }

    public static RecommendTagInfoDto json2RecommendTagInfo(JSONObject o){
        RecommendTagInfoDto tagInfo=new RecommendTagInfoDto(o.getString("tag_id"),o.getString("tag_name"));
        return tagInfo;
    }

    /**
     *
     * @param img Image
     * @return 先转换成ImageTag,再转json
     */
    public static String json2Image(Image img){
        ObjectMapper mapper = new ObjectMapper();
        ImageTag imageTag=ImageTag.turntoImageTag(img);
        return imageTag.buildJson();

    }

}
